home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Language/OS - Multiplatform Resource Library
/
LANGUAGE OS.iso
/
cpp_libs
/
answrbok
/
3_11.lha
/
3_11
/
3_11d.c
< prev
next >
Wrap
C/C++ Source or Header
|
1993-08-08
|
679b
|
39 lines
* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
* The C++ Answer Book */
* Tony Hansen */
* All rights reserved. */
/ convert an integer into a string of max length len
/ version 1
include <swap.h>
include <error.h>
oid itoa(char *s, int len, int val)
// reverse the sign of negative numbers
if (val < 0)
{
if (--len <= 0)
error("string not long enough");
*s++ = '-';
val = -val;
}
// store digits in reverse order
char *p = s;
do {
if (--len <= 0)
error("string not long enough");
*p++ = val % 10 + '0';
val /= 10;
} while (val != 0);
*p-- = '\0';
// swap the digits around
while (s < p)
swap(*s++, *p--);